<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共{{ total }}条记录</span>
          <el-button icon="el-icon-plus" size="small" type="primary" round @click="openDrawer">
            添加面经
          </el-button>
        </div>
      </template>
      <!-- 表格部分  -->
      <el-table v-loading="isLoading" :data="tableData" stripe style="width: 100%">
        <el-table-column prop="stem" label="标题" width="400"></el-table-column>
        <el-table-column prop="creator" label="作者"></el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"></el-table-column>
        <el-table-column prop="createdAt" label="更新时间" width="200"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template #default="{ row }">
            <div class="actions">
              <i class="el-icon-view" @click="openDrawer('preview',row.id)"></i>
              <i class="el-icon-edit-outline" @click="openDrawer('edit',row.id)"></i>
              <i class="el-icon-delete" @click="del(null,row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background @current-change="onPageChange" :current-page="current" :page-size="pageSize"
        layout="prev, pager, next" :total="total">
      </el-pagination>
    </el-card>

    <el-drawer
      :visible.sync="isShowDrawer"
      :before-close="handleClose"
      title="大标题"
      direction="rtl"
      size="60%">
          <div v-if="actionType === 3" class="article-preview">
          <h5>{{ formData.stem }}</h5>
          <div v-html="formData.content"></div>
        </div>
      <el-form v-else ref="form" label-width="80px" :model="formData" :rules="rules">
      <el-form-item label="标题" prop="stem">
        <el-input v-model="formData.stem" @blur="$refs.form.validateField('stem')" placeholder="输入面经标题"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
      <quill-editor v-model="formData.content" ref="myQuillEditor" @blur="$refs.form.validateField('content')">
      </quill-editor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="doSubmit">确认</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>

    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

import { getArticleList, deleteArticleList, createArticle, getArticleDetail } from '@/api/article'
export default {
  name: 'ArticleIndex',
  components: {
    quillEditor
  },
  data () {
    return {
      isLoading: false,
      tableData: [],
      // 页数：
      current: 1,
      pageSize: 10,
      // 总数据条数（调接口以后获得）
      total: 0,
      isShowDrawer: false,
      formData: {
        content: '',
        stem: ''
      },
      actionType: null
    }
  },
  async created () {
    this.getArticles()
    this.rules = { title: [{ required: true, message: '请输入标题' }], content: [{ required: true, message: '请输入内容' }] }
  },
  methods: {
    async getArticles () {
      // 请求之前有遮罩
      this.isLoading = true
      const res = await getArticleList(this.current, this.pageSize)
      // 请求之后无遮罩
      this.isLoading = false
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    onPageChange (page) {
      this.current = page
      console.log(page)
      this.getArticles()
    },
    // 删除（当前行数据）
    async del (id) {
      await deleteArticleList(id)
      // 提示用户，删除成功
      // this.$message.success('删除成功')
      // 判断是否是最后一个数据
      if (this.tableData.length === 1 && this.current > 1) {
        this.current--
      }
      // 重新渲染
      this.getArticles()
    },
    handleClose (done) {
      done()
      this.closeDrawer()
    },
    closeDrawer () {
      this.drawer = false
      this.$refs.form.resetFields()
    },
    // 手动校验
    async doSubmit () {
      // 1.表单手动校验
      await this.$refs.form.validate()
      // 2.调接口，发请求，创建面经/
      await createArticle({ stem: this.formData.title }, { content: this.formData.content })
      await createArticle(this.formData)
      // 3.提示用户，关闭抽眶 this.$message.success('面经创建成功')
      this.closeDrawer()
      // 4.刷新当前的表格（最好是刷第一页)
      this.current = this.getArticles()
    },
    async openDrawer (actionType = 'add', id) {
      this.isShowDrawer = true
      // 如果id有值，说明是编辑
      if (actionType === 'edit') {
        // 使用id参数来获取面经详情==>回显到表单中(M=>V)
        const res = await getArticleDetail(id)
        this.formData.stem = res.data.stem
        this.formData.content = res.data.content
        this.actionType = null
      } else if (actionType === 'preview') {
        const res = await getArticleDetail(id)
        this.formData.stem = res.data.stem
        this.formData.content = res.data.content
        this.actionType = 3
      } else {
        this.formData.stem = ''
        this.formData.content = ''
        this.actionType = null
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }

  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;

    >i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}

.el-breadcrumb {
  margin-top: 10px;
}

.el-form {
  padding-right: 40px;
}

.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}

.el-rate {
  padding: 10px 0;
}

.article-preview {
  padding: 0 40px 40px 40px;

  >h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
